<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>结果下载 - RDK X5智能办公系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            padding-top: 56px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            background-color: #f8f9fa;
        }
        .content {
            flex: 1;
            padding: 2rem 0;
        }
        .footer {
            background-color: #343a40;
            color: #fff;
            padding: 1.5rem 0;
            margin-top: auto;
        }
        .folder-container {
            margin-bottom: 1.5rem;
            border-radius: 0.5rem;
            overflow: hidden;
            box-shadow: 0 0.125rem 0.25rem rgba(0,0,0,0.075);
        }
        .folder-tree-item {
            border: 1px solid #e9ecef;
            border-radius: 0.375rem;
            margin-bottom: 0.5rem;
            background-color: #fff;
        }
        .folder-header {
            background-color: #f8f9fa;
            padding: 12px 15px;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .folder-header:hover {
            background-color: #e9ecef;
        }
        .folder-header h5 {
            margin: 0;
            display: flex;
            align-items: center;
            font-size: 1rem;
        }
        .folder-header h5 i {
            margin-right: 8px;
            color: #007bff;
        }
        .folder-content {
            transition: all 0.3s ease;
            display: none; /* 默认隐藏 */
        }
        .folder-content.collapsed {
            display: none;
        }
        .folder-content.expanded {
            display: block; /* 展开时显示 */
        }
        .file-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 15px;
            border-bottom: 1px solid #f1f3f4;
            background-color: #fdfdfd;
            transition: background-color 0.2s;
        }
        .file-item:hover {
            background-color: #f8f9fa;
        }
        .file-item:last-child {
            border-bottom: none;
        }
        .file-info {
            flex-grow: 1;
        }
        .file-info i {
            margin-right: 6px;
            color: #6c757d;
        }
        .file-actions {
            white-space: nowrap;
        }
        .empty-state {
            text-align: center;
            padding: 50px 0;
            color: #6c757d;
        }
        .empty-state i {
            font-size: 48px;
            margin-bottom: 15px;
            color: #adb5bd;
        }
        .btn-group-actions {
            display: flex;
            gap: 5px;
        }
        /* 文件夹层级缩进 */
        .depth-0 { margin-left: 0px; }
        .depth-1 { margin-left: 20px; }
        .depth-2 { margin-left: 40px; }
        .depth-3 { margin-left: 60px; }
        .depth-4 { margin-left: 80px; }
        .depth-5 { margin-left: 100px; }
        .depth-6 { margin-left: 120px; }
        .depth-7 { margin-left: 140px; }
        .depth-8 { margin-left: 160px; }
        .depth-9 { margin-left: 180px; }
        .depth-10 { margin-left: 200px; }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
        <div class="container">
            <a class="navbar-brand" href="/">RDK X5智能办公系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/">文档上传</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/process">文档处理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/download">文件下载</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 内容区 -->
    <div class="container content">
        <div class="row">
            <div class="col-12">
                <h1 class="display-5 mb-4">结果下载中心</h1>
                <p class="lead mb-4">查看并下载文档处理的结果文件，支持单个文件下载和整个文件夹打包下载。</p>
                
                <div class="card">
                    <div class="card-header bg-light d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">处理结果文件</h5>
                        <button id="refreshResultsBtn" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-arrow-clockwise"></i> 刷新
                        </button>
                    </div>
                    <div class="card-body">
                        {% if results %}
                            <!-- 递归显示嵌套文件夹结构的宏 -->
                            {% macro render_folder_tree(tree, depth=0, parent_path="") %}
                                {% set folder_id = (parent_path + "/" + tree.name if parent_path else tree.name)|replace("/", "_")|replace(" ", "_") %}
                                <div class="folder-tree-item depth-{{ depth }}">
                                    <div class="folder-header" data-folder="{{ tree.name }}">
                                        <h5>
                                            <i class="bi bi-folder{{ '-open' if tree.folders or tree.files else '' }}"></i>
                                            {{ tree.name }}
                                        </h5>
                                        <div class="btn-group-actions">
                                            {% if depth == 0 %}
                                                <button class="btn btn-sm btn-success download-folder" data-folder="{{ tree.name }}">
                                                    <i class="bi bi-download"></i> 打包下载
                                                </button>
                                                <button class="btn btn-sm btn-danger delete-folder" data-folder="{{ tree.name }}">
                                                    <i class="bi bi-trash"></i> 删除文件夹
                                                </button>
                                            {% endif %}
                                            {% if tree.folders or tree.files %}
                                                <button class="btn btn-sm btn-primary toggle-folder" data-target="folder-content-{{ folder_id }}">
                                                    <i class="bi bi-chevron-down folder-toggle-icon"></i>
                                                </button>
                                            {% endif %}
                                        </div>
                                    </div>
                                    
                                    {% if tree.folders or tree.files %}
                                        <div class="folder-content collapsed" id="folder-content-{{ folder_id }}">
                                            <!-- 显示文件 -->
                                            {% for file in tree.files %}
                                                <div class="file-item depth-{{ depth + 1 }}">
                                                    <div class="file-info">
                                                        <div class="fw-semibold">
                                                            <i class="bi bi-file-text"></i>
                                                            {{ file.name }}
                                                            {% if file.name.endswith('_总结.md') %}
                                                                <span class="badge bg-info">总结</span>
                                                            {% elif file.name.endswith('_分点提炼.md') %}
                                                                <span class="badge bg-success">提炼</span>
                                                            {% elif file.name.endswith('_中译英.md') %}
                                                                <span class="badge bg-warning">中译英</span>
                                                            {% elif file.name.endswith('_英译中.md') %}
                                                                <span class="badge bg-warning">英译中</span>
                                                            {% endif %}
                                                        </div>
                                                        <small class="text-muted">
                                                            {{ (file.size / 1024)|round(1) }} KB | {{ file.modified }}
                                                        </small>
                                                    </div>
                                                    <div class="file-actions">
                                                        <a href="/api/download-file/{{ file.path }}" class="btn btn-sm btn-outline-primary">
                                                            <i class="bi bi-download"></i> 下载
                                                        </a>
                                                        <button class="btn btn-sm btn-outline-danger delete-file" data-file="{{ file.path }}">
                                                            <i class="bi bi-trash"></i> 删除
                                                        </button>
                                                    </div>
                                                </div>
                                            {% endfor %}
                                            
                                            <!-- 递归显示子文件夹 -->
                                            {% for subfolder in tree.folders %}
                                                {{ render_folder_tree(subfolder, depth + 1, parent_path + "/" + tree.name if parent_path else tree.name) }}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                </div>
                            {% endmacro %}
                            
                            <!-- 显示所有结果文件夹 -->
                            {% for result in results %}
                                <div class="folder-container">
                                    {{ render_folder_tree(result.tree) }}
                                </div>
                            {% endfor %}
                        {% else %}
                            <div class="empty-state">
                                <i class="bi bi-folder-x"></i>
                                <h5>暂无处理结果文件</h5>
                                <p>您可以在"文档处理"页面处理文档，处理完成后的结果将显示在这里</p>
                                <a href="/process" class="btn btn-primary">去处理文档</a>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="text-center">
                <p class="mb-0">RDK X5智能办公系统 &copy; 2025</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const refreshResultsBtn = document.getElementById('refreshResultsBtn');
            
            // 文件夹展开/收起
            document.querySelectorAll('.toggle-folder').forEach(button => {
                button.addEventListener('click', function(e) {
                    e.stopPropagation();
                    const targetId = this.getAttribute('data-target');
                    const folderContent = document.getElementById(targetId);
                    const toggleIcon = this.querySelector('.folder-toggle-icon');
                    
                    if (folderContent.classList.contains('collapsed')) {
                        folderContent.classList.remove('collapsed');
                        folderContent.classList.add('expanded');
                        toggleIcon.classList.remove('bi-chevron-down');
                        toggleIcon.classList.add('bi-chevron-up');
                    } else {
                        folderContent.classList.remove('expanded');
                        folderContent.classList.add('collapsed');
                        toggleIcon.classList.remove('bi-chevron-up');
                        toggleIcon.classList.add('bi-chevron-down');
                    }
                });
            });
            
            // 点击文件夹头部也可以展开/收起
            document.querySelectorAll('.folder-header').forEach(header => {
                header.addEventListener('click', function(e) {
                    // 确保不是点击按钮
                    if (e.target.closest('.btn') || e.target.closest('.btn-group-actions')) {
                        return;
                    }
                    
                    const toggleBtn = this.querySelector('.toggle-folder');
                    if (toggleBtn) {
                        toggleBtn.click();
                    }
                });
            });
            
            // 打包下载文件夹
            document.querySelectorAll('.download-folder').forEach(button => {
                button.addEventListener('click', function(e) {
                    e.stopPropagation();
                    const folder = this.getAttribute('data-folder');
                    window.location.href = `/api/download-folder/${folder}`;
                });
            });
            
            // 删除文件夹
            document.querySelectorAll('.delete-folder').forEach(button => {
                button.addEventListener('click', function(e) {
                    e.stopPropagation();
                    const folder = this.getAttribute('data-folder');
                    
                    if (confirm(`确定要删除 ${folder} 文件夹及其所有内容吗？`)) {
                        fetch(`/api/delete-folder/${folder}`, {
                            method: 'POST'
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                this.closest('.folder-container').remove();
                                
                                // 检查是否还有其他文件夹，如果没有，显示空状态
                                if (document.querySelectorAll('.folder-container').length === 0) {
                                    document.querySelector('.card-body').innerHTML = `
                                        <div class="empty-state">
                                            <i class="bi bi-folder-x"></i>
                                            <h5>暂无处理结果文件</h5>
                                            <p>您可以在"文档处理"页面处理文档，处理完成后的结果将显示在这里</p>
                                            <a href="/process" class="btn btn-primary">去处理文档</a>
                                        </div>
                                    `;
                                }
                            } else {
                                alert(`删除失败: ${data.error}`);
                            }
                        })
                        .catch(error => {
                            alert('删除请求失败');
                            console.error(error);
                        });
                    }
                });
            });
            
            // 删除单个文件
            document.querySelectorAll('.delete-file').forEach(button => {
                button.addEventListener('click', function(e) {
                    e.stopPropagation();
                    const filePath = this.getAttribute('data-file');
                    const fileName = filePath.split('/').pop();
                    
                    if (confirm(`确定要删除文件 ${fileName} 吗？`)) {
                        fetch(`/api/delete-file/${filePath}`, {
                            method: 'POST'
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                const fileItem = this.closest('.file-item');
                                fileItem.remove();
                                
                                // 检查文件夹中是否还有文件
                                const folderContent = this.closest('.folder-content');
                                if (folderContent.querySelectorAll('.file-item').length === 0) {
                                    // 如果没有文件了，也删除文件夹
                                    const folderId = folderContent.id.replace('folder-', '');
                                    fetch(`/api.delete-folder/${folderId}`, {
                                        method: 'POST'
                                    })
                                    .then(() => {
                                        const folderContainer = folderContent.closest('.folder-container');
                                        folderContainer.remove();
                                        
                                        // 检查是否还有其他文件夹
                                        if (document.querySelectorAll('.folder-container').length === 0) {
                                            document.querySelector('.card-body').innerHTML = `
                                                <div class="empty-state">
                                                    <i class="bi bi-folder-x"></i>
                                                    <h5>暂无处理结果文件</h5>
                                                    <p>您可以在"文档处理"页面处理文档，处理完成后的结果将显示在这里</p>
                                                    <a href="/process" class="btn btn-primary">去处理文档</a>
                                                </div>
                                            `;
                                        }
                                    });
                                }
                            } else {
                                alert(`删除失败: ${data.error}`);
                            }
                        })
                        .catch(error => {
                            alert('删除请求失败');
                            console.error(error);
                        });
                    }
                });
            });
            
            // 刷新结果列表
            refreshResultsBtn.addEventListener('click', function() {
                window.location.reload();
            });
        });
    </script>
</body>
</html>